package com.xixing.weixin.response;

import com.thoughtworks.xstream.XStream;
import com.xixing.weixin.domain.Weixin;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <xml>
 * <ToUserName><![CDATA[toUser]]></ToUserName>
 * <FromUserName><![CDATA[fromUser]]></FromUserName>
 * <CreateTime>12345678</CreateTime>
 * <MsgType><![CDATA[news]]></MsgType>
 * <ArticleCount>2</ArticleCount>
 * <Articles>
 * <item>
 * <Title><![CDATA[title1]]></Title>
 * <Description><![CDATA[description1]]></Description>
 * <PicUrl><![CDATA[picurl]]></PicUrl>
 * <Url><![CDATA[url]]></Url>
 * </item>
 * <item>
 * <Title><![CDATA[title]]></Title>
 * <Description><![CDATA[description]]></Description>
 * <PicUrl><![CDATA[picurl]]></PicUrl>
 * <Url><![CDATA[url]]></Url>
 * </item>
 * </Articles>
 * <FuncFlag>1</FuncFlag>
 * </xml>
 */
@RooJavaBean
@RooToString
public class ArticleResponse extends Response {
    private int articleCount;
    private List<ArticleItem> articles = new ArrayList<ArticleItem>();

    public ArticleResponse() {
        setMsgType("news");
    }

    @Override
    protected void prepare(XStream xstream) {
        super.prepare(xstream);
        alias(xstream, "item", ArticleItem.class);
        articleCount = articles.size();
    }

    public static ArticleResponse SingleImage(Weixin weixin, String imageUrl, String title) {
        ArticleResponse result = new ArticleResponse();
        result.setToUserName(weixin.getFromUserName());
        result.setFromUserName(weixin.getToUserName());
        result.setCreateTime(new Date().getTime());
        result.setFuncFlag(0);
        result.getArticles().add(new ArticleItem());
        result.getArticles().get(0).setPicUrl(imageUrl);
        result.getArticles().get(0).setTitle(title);
        result.getArticles().get(0).setUrl(imageUrl);
        result.getArticles().get(0).setDescription(title);
        return result;
    }

    public static ArticleResponse create(Weixin weixin,List<ArticleItem> articleItems){
        ArticleResponse result = new ArticleResponse();
        result.setToUserName(weixin.getFromUserName());
        result.setFromUserName(weixin.getToUserName());
        result.setCreateTime(new Date().getTime());
        result.setFuncFlag(0);
        result.setArticles(articleItems);
        return result;
    }
}
